/*
 * @Author: your name
 * @Date: 2020-08-23 19:16:40
 * @LastEditTime: 2020-08-28 08:40:06
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \新建文件夹\two-app\src\utils\request.js
 */
import axios from 'axios'
import cookie from 'js-cookie'
// import { Message } from 'view-design'
import router from '../router'
// import ErrorCodeError from '../config/error'
// 设置白名单
const writeList = ['/login']
axios.defaults.baseURL = 'http://localhost:8080/api'
// 添加请求拦截
axios.interceptors.request.use(function (config) {
  // 获取token
  const token = cookie.get('token')
  if (!writeList.includes(config.url)) {
    if (!token) {
      router.replace('/login')
    }
    config.headers.authorization = token
  }
  return config
}, function (error) {
  return Promise.reject(error)
})

// 添加响应拦截
axios.interceptors.response.use(function (response) {
  return response
}, function (error) {
  // 错误判断
  // Message.error(ErrorCodeError[error.response.status])
  return Promise.reject(error)
})
export default axios
